﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;


#region 配置文件

public class ConfigDic
{

    public static string ApplicationPath = Application.StartupPath;
    public static string configFileName=ApplicationPath + "\\config.txt";
    public static Dictionary<string, string> ConfigTable = new Dictionary<string, string>(); 

    public static void getConfigTable()
    {
        try
        {
            Int32 i;
            string[] arrS = FileOperate.ReadFile(configFileName).Split('\n');
            ConfigTable.Clear();
            for (i = 0; i < arrS.Length; i++)
            {
                try
                {
                    arrS[i] = arrS[i].Replace("\r", "").Trim();
                    if (arrS[i].Length > 0)
                    {
                        string[] arrS2 = arrS[i].Split('=');
                        if ((arrS2.Length >= 2))
                        {
                            string key = arrS2[0];
                            string truevalue = "";
                            for (Int32 j = 1; j < arrS2.Length; j++)
                            {
                                truevalue += arrS2[j];
                                if (j < arrS2.Length - 1)
                                {
                                    truevalue += "=";
                                }
                            }
                            ConfigTable.Add(key, truevalue);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Record(ex);
                }
            }
        }
        catch (Exception ex)
        {
            Log.Record(ex);
        }
    }

    public static void saveConfigTable()
    {
        try
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder(400);

            foreach (KeyValuePair<string, string> item in ConfigTable)
            {
                sb.Append(item.Key);
                sb.Append("=");
                sb.Append(item.Value);
                sb.Append("\r\n");
            }


            FileOperate.WriteFile(configFileName, sb.ToString());
        }
        catch (Exception ex)
        {
            Log.Record(ex);
        }
    }
}
#endregion

#region 记录错误日志

// 记录错误日志
public class Log {

    public static string exceptionFileName = Application.StartupPath + "\\exception.log";
    
    public static void Record(System.Exception ex) {
        try {
            //清空日志
            if (System.IO.File.Exists(exceptionFileName))
            {
                System.IO.FileInfo myExceptionLogInfo = new System.IO.FileInfo(exceptionFileName);
                if ((myExceptionLogInfo.Length > 100000)) {
                    FileOperate.WriteFile(exceptionFileName, "");
                }
            }
            System.IO.StreamWriter sw = new System.IO.StreamWriter(exceptionFileName, true, System.Text.Encoding.UTF8);
            sw.WriteLine("#######################################");
            sw.WriteLine(("message:" + ex.Message));
            sw.WriteLine(("Source:" + ex.Source));
            sw.WriteLine(("StackTrace:" + ex.StackTrace));
            sw.WriteLine(("DateTime:" + DateTime.Now.ToString()));
            sw.WriteLine("#######################################");
            sw.Close();
        }
        catch {
        }
    }
    
    public static void Record(string message) {
        try {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(exceptionFileName, true, System.Text.Encoding.UTF8);
            sw.WriteLine("#######################################");
            sw.WriteLine(("message:" + message));
            sw.WriteLine(("DateTime:" + DateTime.Now.ToString()));
            sw.WriteLine("#######################################");
            sw.Close();
        }
        catch {
        }
    }
    
    public static void Record(System.Exception ex, string message) {
        try {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(exceptionFileName, true, System.Text.Encoding.UTF8);
            sw.WriteLine("#######################################");
            sw.WriteLine(("message:" + ex.Message));
            sw.WriteLine(("Source:" + ex.Source));
            sw.WriteLine(("StackTrace:" + ex.StackTrace));
            sw.WriteLine(("errorinfo:" + message));
            sw.WriteLine(("DateTime:" + DateTime.Now.ToString()));
            sw.WriteLine("#######################################");
            sw.Close();
        }
        catch {
        }
    }

}
#endregion

#region 读写文件

public class FileOperate
{
    //读文件
    public static string ReadFile(string fileName)
    {
        string fileContent = "";
        try
        {
            System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.StreamReader sr = new System.IO.StreamReader(fs, System.Text.Encoding.UTF8);
            fileContent = sr.ReadToEnd();
            sr.Close();
            fs.Close();
        }
        catch 
        {
        }
        return fileContent;
    }

    //写文件
    public static void WriteFile(string fileName, string wl)
    {
        try
        {
            System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
            sw.Write(wl);
            sw.Close();
            fs.Close();
        }
        catch 
        {
        }
    }
}

#endregion

